## FocusXml - Handle XML parsing related activities
## Copyright (C) January 26, 2005 Erick P. Bodine
## email: erick_bodine@comcast.net
## webaddress: 

## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# TODO: Finish necessary functions


"""
NAME
FocusXml

DESCRIPTION
An XML rendition of the MyFocus backend

AUTHOR
Erick P Bodine
"""

from xml.parsers import expat

# Try to import the faster cElementTree first
try: 
    import cElementTree as ElementTree
except ImportError: 
    from elementtree import ElementTree

__author__ = "$Author: ebodine $"
__date__ = "$Date: 2005-03-13 21:30:04 -0700 (Sun, 13 Mar 2005) $"
__version__ = "$Revision: 8 $"


class FocusXML:
    def __init__(self, xml_file):
        self.xml_file = xml_file
        self.root = self._get_root()

    def _get_root(self):
        """ Return the root element from the xml file. """
        try:
            return ElementTree.ElementTree(file=self.xml_file)
        except expat.ExpatError, error:
            print "ExpatError: ", error
            
            
    def getProjectNameList(self):
        projectsList = []
        
        projects = self.root.findall('Project')
        for p in projects:
            for name,value in p.items():
                projectsList.append(value)
                
        return projectsList
        
    def getTaskList(self, projectName):
        
        task_data = {}
        projects = self.root.findall('Project')
        for p in projects:
            for name,value in p.items():
                if value == projectName:
                    for child in p.getchildren():
                        if child.tag == 'Task':
                            for name,value in child.items():
                                task_data[name] = value
                            for c in child.getchildren():
                                task_data[c.tag] = c.text
                                
                    break
                else:
                    continue
                    
        return task_data
        
if __name__ == '__main__':
    test_xml_file = 'focus.test.xml'
    X = FocusXML(test_xml_file)
    
    print X.getProjectNameList()
    print X.getTaskList('Project1')
    
#    for element in X.root.getiterator():
#        if element.tag == 'Project':
#        if element.keys():
#            for name,value in element.items():
#                print "Name: %s, Value: %s" % (name, value)
